package service.auth.biz;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.onewaveinc.dolphin.portal.token.service.PortalToken;

import service.auth.config.AppConstants;
import service.auth.dao.StaticData;
import service.auth.dao.TSApplicationDao;
import service.auth.po.TSApplication;
import service.auth.utils.AESUtils;
import service.auth.utils.ThreeDESUtil;
@Service
public class AuthBiz {
	protected static String TAG="AuthBiz";
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private TSApplicationDao appDao;
	/**
	 * 解码请求应用信息
	 * @param accessKey (appId,appKey,requestTimeStamp)
	 * @param decodeType
	 */
	private String decode(String accessKey, String decodeType){
		logger.info(TAG+": decode(accessKey:"+accessKey+", decodeType:"+decodeType+") start");
		String appInfoStr=null;
		if(AppConstants.ENCRY_TYPE_AES.equals(decodeType)){
			logger.info(TAG+": AES decode start");
			appInfoStr=AESUtils.decrypt(AppConstants.SEED, accessKey);
			logger.info(TAG+": AES decode end");
		}else if(AppConstants.ENCRY_TYPE_DES.equals(decodeType)){
			logger.info(TAG+": DES decode start");
			ThreeDESUtil td = new ThreeDESUtil();
			appInfoStr=td.getDecString(accessKey, AppConstants.SEED);
			logger.info(TAG+": DES decode end");
		}
		logger.info(TAG+": decode() end");
		return appInfoStr;
	}
	/**
	 * 生成cdn token
	 * @return
	 */
	public String generateCDNToken(){
		logger.info(TAG+": generateCDNToken() start");
		String cdnToken=PortalToken.genPortalToken(AppConstants.CDN_KEY, AppConstants.CDN_TIME);//AppConstants.CDN_TIME);
		logger.info(TAG+": generateCDNToken() end");
		return cdnToken;
	}
	/**
	 * 加密
	 * @param appId
	 * @param cdnToken
	 * @param responseTimeStamp
	 * @param decodeType
	 * @return
	 */
	private String encode(String appId, String cdnToken, long responseTimeStamp, String decodeType){
		logger.info(TAG+": encode(appId:"+appId+",cdnToken:"+cdnToken+",responseTimeStamp:"+responseTimeStamp+",decodeType:"+decodeType+") start");
		String tokenStr=appId+AppConstants.SPLIT_CHAR+cdnToken+AppConstants.SPLIT_CHAR+responseTimeStamp;
		String secretToken=null;
		if(AppConstants.ENCRY_TYPE_AES.equals(decodeType)){
			logger.info(TAG+": AES encode start");
			secretToken=AESUtils.encrypt(AppConstants.SEED, tokenStr);
			logger.info(TAG+": AES encode end");
		}else if(AppConstants.ENCRY_TYPE_DES.equals(decodeType)){
			logger.info(TAG+": DES encode start");
			ThreeDESUtil td = new ThreeDESUtil();
			secretToken=td.getEncString(tokenStr, AppConstants.SEED);
			logger.info(TAG+": DES encode end");
		}
		logger.info(TAG+": encode() end");
		return secretToken;
	}
	/**
	 * 验证appId 并响应门户 token
	 * @param accessKey
	 * @param decodeType
	 * @return
	 */
	public String auth(String accessKey, String decodeType){
		logger.info(TAG+": auth(accessKey:"+accessKey+",decodeType:"+decodeType+") start");
		try {
			String appInfoStr=decode(accessKey, decodeType);
			String appId=null;
			String appKey=null;
			long requestTimeStamp=0;
			//分割字符串 取出appId,appKey,timeStamp
			logger.info("分割字符串 取出appId,appKey,timeStamp");
			if(appInfoStr!=null){
				String[] appInfoStrs=appInfoStr.split(AppConstants.SPLIT_CHAR);
				if(appInfoStrs.length==3){
					appId=appInfoStrs[0];
					appKey=appInfoStrs[1];
					requestTimeStamp=new Long(appInfoStrs[2]);
				}
			}
			logger.info("验证app");
			//验证app
			if(appId!=null){
				long currentTimeStamp=System.currentTimeMillis();
				//验证请求是否过期
				logger.info("验证请求是否过期");
				if(1>0||currentTimeStamp-requestTimeStamp<AppConstants.REQUEST_ACTIVE_TIME*60*1000){
					TSApplication app=StaticData.appMap.get(appId);
					if(app==null){
						app=appDao.selectByPrimaryKey(appId);
						StaticData.appMap.put(appId, app);
					}
					//验证key
					logger.info("验证key");
					if(app!=null&&app.getAppKey()!=null&&app.getAppKey().equals(appKey)){
						String cdnToken=generateCDNToken();
						long respTimeStamp=System.currentTimeMillis();
						return this.encode(appId, cdnToken, respTimeStamp, decodeType);
					}else{
						logger.info("验证key失败");
					}
				}else{
					logger.info("请求过期");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage()+" \n");
		} finally{
			logger.info(TAG+": auth(accessKey:"+accessKey+",decodeType:"+decodeType+") end");
		}
		return null;
	}
	
	public static void main(String[] args) {
		AuthBiz authBiz=new AuthBiz();
		System.out.println(authBiz.generateCDNToken());
	}
}
